package com.jmx.yp.struts.member;

import net.sf.json.JSONObject;

import com.jmx.yp.common.SysConstants;
import com.jmx.yp.entity.EmpGrade;
import com.jmx.yp.entity.GroupEmployee;
import com.jmx.yp.exception.ErrorCode;
import com.jmx.yp.exception.HandlerException;

import org.apache.log4j.*;
import com.jmx.yp.service.member.IGradeService;
import com.jmx.yp.service.member.ICategoryService;
import com.opensymphony.xwork2.ActionContext;

import java.util.*;

/**
 * 
 * 功能描述：
 *			会员级别业务控制
 * @author Jenny
 *
 * Date:2011-2-16 下午05:52:16
 */
public class GradeAction {
	private static Logger log = (Logger)Logger.getLogger(GradeAction.class);
	
	private IGradeService gradeService;
	
	private ICategoryService categoryService;
	
	public void setGradeService(IGradeService gradeService){
		this.gradeService = gradeService;
	}
	
	public void setCategoryService(ICategoryService categoryService){
		this.categoryService = categoryService;
	}
	
	/**
	 * 查询级别列表：入口
	 * @return
	 */
	public String initGradeList(){
		String forward = "error";
		try{
			//初始化界面会员类别列表
			categoryList = categoryService.queryCategory(null);
			forward = "queryGrade";
		}catch(Exception e){
			e.printStackTrace();
			log.error("GradeAction initGradeList error: " + e.getMessage());
		}
		
		return forward;
	}
	
	/**
	 * 查询会员级别列表
	 * @return
	 */
	public String queryGrade(){
		try{
			Map map = gradeService.queryGrade(queryName, queryCategoryId, nowPage);
			
			result = JSONObject.fromObject(map);
	System.out.println(result.toString());
		}catch(Exception e){
			e.printStackTrace();
			log.error("GradeAction queryGrade error: " + e.getMessage());
		}
		return "success";
	}
	
	/**
	 * 新增会员级别：入口
	 * @return
	 */
	public String initAddGrade(){
		String forward = "error";
		try{
			//初始化界面会员类别列表
			categoryList = categoryService.queryCategory(null);
			forward = "addGrade";
		}catch(Exception e){
			e.printStackTrace();
			log.error("GradeAction initAddGrade error: " + e.getMessage());
		}
		return forward;
	}
	
	/**
	 * 新增会员级别
	 * 		说明：同一会员类别下不允许有相同名称
	 * @return
	 */
	public String addGrade(){
		try{
			EmpGrade grade = assembleGrade();
			gradeService.addGrade(grade);
			results = "success";
		}catch(HandlerException he){
			results = he.toString();
		}catch(Exception e){
			e.printStackTrace();
			log.error("GradeAction addGrade error: " + e.getMessage());
			results = ErrorCode.COMMON_ERROR;
		}
		return "success";
	}
	
	private EmpGrade assembleGrade(){
		GroupEmployee employee = (GroupEmployee)ActionContext.getContext().getSession().get("user");
		EmpGrade grade = new EmpGrade();
		if(serialid != null)
			grade.setSerialid(Long.parseLong(serialid));
		grade.setName(name.trim());
		grade.setCategoryId(categoryId);
		grade.setDescription(description);
		
		grade.setInvalid(SysConstants.VALID);
		grade.setOperateTime(new Date());
		grade.setOperator(employee.getGrpemp_id());
		grade.setState(SysConstants.STATE_NORMAL);
		
		return grade;
	}
	
	/**
	 * 会员级别详情
	 * @return
	 */
	public String detailGrade(){
		String forward = "error";
		try{
			EmpGrade grade = gradeService.getGrade(Long.parseLong(serialid));
			categoryList = categoryService.queryCategory(null);
			setProperties(grade);
			
			forward = "detailGrade";
		}catch(Exception e){
			e.printStackTrace();
			log.error("GradeAction detailGrade error: " + e.getMessage());
		}
		
		return forward;
	}
	
	/**
	 * 设置属性值
	 * @param grade
	 */
	private void setProperties(EmpGrade grade){
		serialid = grade.getSerialid().toString();
		name = grade.getName();
		description = grade.getDescription();
		categoryId = grade.getCategoryId();
	}
	
	/**
	 * 修改会员级别：入口
	 * @return
	 */
	public String initUpdateGrade(){
		String forward = "error";
		try{
			EmpGrade grade = gradeService.getGrade(Long.parseLong(serialid));
			categoryList = categoryService.queryCategory(null);
			setProperties(grade);
			
			forward = "updateGrade";
		}catch(Exception e){
			e.printStackTrace();
			log.error("GradeAction initUpdateGrade error: " + e.getMessage());
		}
		
		return forward;
	}
	
	/**
	 * 修改会员级别
	 * 	  说明：目前允许进行修改而无限制，暂未加若已关联相关会员不允许修改会员类别
	 * @return
	 */
	public String updateGrade(){
		try{
			EmpGrade grade = assembleGrade();
			gradeService.updateGrade(grade);
			
			results = "success";
		}catch(HandlerException he){
			results = he.toString();
		}catch(Exception e){
			e.printStackTrace();
			log.error("GradeAction updateGrade error: " + e.getMessage());
			results = ErrorCode.COMMON_ERROR;
		}
		return "success";
	}
	
	/**
	 * 删除会员级别
	 * 	 说明：若已经关联会员，则不允许删除
	 * @return
	 */
	public String deleteGrade(){
		GroupEmployee employee = (GroupEmployee)ActionContext.getContext().getSession().get("user");
		
		try{
			gradeService.deleteGrade(serialid, employee.getGrpemp_id());
			results = "success";
		}catch(HandlerException he){
			results = he.toString();
		}catch(Exception e){
			e.printStackTrace();
			log.error("GradeAction deleteGrade error: " + e.getMessage());
			results = ErrorCode.COMMON_ERROR;
		}
		return "success";
	}
	
	/**属性**/
	private String queryName;
	private Long queryCategoryId;
	private int nowPage;
	
	private List categoryList;
	
	private String results;
	private JSONObject result; //保留
	
	private String serialid;
	private String name;
	private String description;
	private Long categoryId;
	
	

	public int getNowPage() {
		return nowPage;
	}

	public void setNowPage(int nowPage) {
		this.nowPage = nowPage;
	}

	public String getSerialid() {
		return serialid;
	}

	public void setSerialid(String serialid) {
		this.serialid = serialid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}

	public String getResults() {
		return results;
	}

	public void setResults(String results) {
		this.results = results;
	}

	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public String getQueryName() {
		return queryName;
	}

	public void setQueryName(String queryName) {
		this.queryName = queryName;
	}

	public Long getQueryCategoryId() {
		return queryCategoryId;
	}

	public void setQueryCategoryId(Long queryCategoryId) {
		this.queryCategoryId = queryCategoryId;
	}

	public List getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List categoryList) {
		this.categoryList = categoryList;
	}
}
